iT邦幫忙

2022 iThome 鐵人賽

DAY 17
0

Review

昨天我們學到了AOP的使用場景,可以把loggin、稽核、效能監控的這些關注面向Cross-cutting concerns抽離出來模組化,讓我們只關注在商業邏輯上

今日我們先用前一天的例子來解釋AOP中的專業術語,再用一個快速入門範例來熟悉Spring AOP的操作

導入JAR

spring-aspects-5.3.22.jar是在spring framework 5.2.22.RELEASE中可以找到,另外3個spring加強版的AOP jar需去maven自行下載引入
https://ithelp.ithome.com.tw/upload/images/20221004/20128084e0lT4kKzG4.jpg

AOP專業術語說明

https://ithelp.ithome.com.tw/upload/images/20221004/20128084b06GTro8Ea.jpg

  • Aspect:A modularization of a concern that cuts across multiple classes.

    代表一個Cross-Cutting Concerns例如logging、稽核、效能監控等,以上圖來說我們關注的就是logging這個切面

  • Advice:Action taken by an aspect at a particular join point

    指的是我們在切入點執行的動作,以上圖來說就是通知方法

  • Pointcut:A predicate that matches join points

    一個預期可以切入的點,以上圖來說我們在logging中關心方法開始、結束、返回、異常這些地方,以上圖來說就是連接點

  • Join point:pointcut expression:A pointcut declaration has two parts: a signature comprising a name and any parameters and a pointcut expression that determines exactly which method executions we are interested in.

    希望那些地方切入執行通知方法,例如上圖(紅色圓圈),我們希望div method方法異常的時侯留個log或是mul method執行的結果是我們在意的點。程式透過pointcut expression的設定來找出join point

Advice Type

  • Before advice:runs before a join point
  • After returning advice:run after a join point completes normally
  • After throwing advice:Advice to be run if a method exits by throwing an exception.
  • After (finally) advice:a join point exits (normal or exceptional return)
  • Around advice: Advice that surrounds a join point such as a method invocation.

Advice Annotation

  • @Before:前置通知
  • @After:後置通知
  • @AfterReturning:返回通知
  • @AfterThrowing:異常通知
  • @Around:環繞通知

快速入門範例

延續前一日範例,改寫為AOP版本

public interface Calculator {
    int add(int i,int j);
    int sub(int i,int j);
    int mul(int i,int j);
    int div(int i,int j);
}
@Service
public class MyCalculator implements Calculator{
    @Override
    public int add(int i, int j) {
        int result = i+j;
        return result;
    }

    @Override
    public int sub(int i, int j) {
        int result = i-j;
        return result;
    }

    @Override
    public int mul(int i, int j) {
        int result = i*j;
        return result;
    }

    @Override
    public int div(int i, int j) {
        int result = i/j;
        return result;
    }
}
@Aspect
@Component
public class LogUtils {
    //想在目標方法之前執行
    @Before("execution(public int com.swj.MyCalculator.*(int,int))")
    public static void logStart(){
        System.out.println("method start");
    }
    //想在目標方法正常執行完成後執行
    @AfterReturning("execution(public int com.swj.MyCalculator.*(int,int))")
    public static void logReturn(){
        System.out.println("method completed,result is");
    }
    @After("execution(public int com.swj.MyCalculator.*(int,int))")
    public static void logEnd(){
        System.out.println("method end");
    }
    //想在目標方法結束後執行
    @AfterThrowing("execution(public int com.swj.MyCalculator.*(int,int))")
    public static void logException(){
        System.out.println("method throws exception");
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.swj"></context:component-scan>
    <!-- 啟動AOP   -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
@Test
public void testDay18(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean18.xml");
    System.out.println("容器啟動完成....");
    Calculator calculator = ioc.getBean(Calculator.class);
    calculator.add(1,1);
    System.out.println("=========");
    calculator.div(1,0);
}

Result
https://ithelp.ithome.com.tw/upload/images/20221004/20128084AcZHgF05OW.jpg

Reference


上一篇
Day17 - AOP 初探
下一篇
Day19 - AOP (1)
系列文
這些年,我們早該學會的Spring Framework30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言